From 2fe0bf83bf78b0064e5bfd9bcab64a51ae48951e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 18 May 2015 15:33:04 -0700 Subject: [PATCH] Remove borrowed pointer in Config Not having a lifetime parameter on the ubiquitous Config structure allows a great deal of other lifetime annotations to be removed and should make dealing with storage of Config in a structure much easier (only one lifetime to deal with, not two). --- src/cargo/core/registry.rs | 14 +++++----- src/cargo/lib.rs | 16 ++++++----- src/cargo/ops/cargo_clean.rs | 4 +-- src/cargo/ops/cargo_compile.rs | 4 +-- src/cargo/ops/cargo_doc.rs | 4 +-- src/cargo/ops/cargo_generate_lockfile.rs | 4 +-- src/cargo/ops/cargo_rustc/context.rs | 10 +++---- src/cargo/ops/cargo_rustc/fingerprint.rs | 22 +++++++-------- src/cargo/ops/cargo_rustc/mod.rs | 34 ++++++++++++------------ src/cargo/ops/cargo_test.rs | 4 +-- src/cargo/sources/git/source.rs | 16 +++++------ src/cargo/sources/path.rs | 20 +++++++------- src/cargo/sources/registry.rs | 14 +++++----- src/cargo/util/config.rs | 10 +++---- src/cargo/util/toml.rs | 4 +-- 15 files changed, 92 insertions(+), 88 deletions(-) diff --git a/src/cargo/core/registry.rs b/src/cargo/core/registry.rs index d6d4e8984..3bd5e6a60 100644 --- a/src/cargo/core/registry.rs +++ b/src/cargo/core/registry.rs @@ -32,9 +32,9 @@ impl Registry for Vec { /// `SourceMap` structure contained within which is a mapping of a `SourceId` to /// a `Source`. Each `Source` in the map has been updated (using network /// operations if necessary) and is ready to be queried for packages. -pub struct PackageRegistry<'a, 'b: 'a> { - sources: SourceMap<'a>, - config: &'a Config<'b>, +pub struct PackageRegistry<'cfg> { + sources: SourceMap<'cfg>, + config: &'cfg Config, // A list of sources which are considered "overrides" which take precedent // when querying for packages. @@ -67,8 +67,8 @@ enum Kind { Normal, } -impl<'a, 'b> PackageRegistry<'a, 'b> { - pub fn new(config: &'a Config<'b>) -> PackageRegistry<'a, 'b> { +impl<'cfg> PackageRegistry<'cfg> { + pub fn new(config: &'cfg Config) -> PackageRegistry<'cfg> { PackageRegistry { sources: SourceMap::new(), source_ids: HashMap::new(), @@ -100,7 +100,7 @@ impl<'a, 'b> PackageRegistry<'a, 'b> { Ok(ret) } - pub fn move_sources(self) -> SourceMap<'a> { + pub fn move_sources(self) -> SourceMap<'cfg> { self.sources } @@ -275,7 +275,7 @@ impl<'a, 'b> PackageRegistry<'a, 'b> { } } -impl<'a, 'b> Registry for PackageRegistry<'a, 'b> { +impl<'cfg> Registry for PackageRegistry<'cfg> { fn query(&mut self, dep: &Dependency) -> CargoResult> { let overrides = try!(self.query_overrides(dep)); diff --git a/src/cargo/lib.rs b/src/cargo/lib.rs index 904f4d48e..d2b01e92c 100644 --- a/src/cargo/lib.rs +++ b/src/cargo/lib.rs @@ -92,16 +92,20 @@ fn process(mut callback: F) where F: FnMut(&[String], &Config) -> CliResult>, V: Encodable { - let mut shell = shell(true); - process_executed((|| { - let config = try!(Config::new(&mut shell)); + let mut config = None; + let result = (|| { + config = Some(try!(Config::new(shell(true)))); let args: Vec<_> = try!(env::args_os().map(|s| { s.into_string().map_err(|s| { human(format!("invalid unicode in argument: {:?}", s)) }) }).collect()); - callback(&args, &config) - })(), &mut shell) + callback(&args, config.as_ref().unwrap()) + })(); + let mut verbose_shell = shell(true); + let mut shell = config.as_ref().map(|s| s.shell()); + let shell = shell.as_mut().map(|s| &mut **s).unwrap_or(&mut verbose_shell); + process_executed(result, shell) } pub fn process_executed(result: CliResult>, shell: &mut MultiShell) @@ -113,7 +117,7 @@ pub fn process_executed(result: CliResult>, shell: &mut MultiShell) let encoded = json::encode(&encodable).unwrap(); println!("{}", encoded); } - _ => {} + Ok(None) => {} } } diff --git a/src/cargo/ops/cargo_clean.rs b/src/cargo/ops/cargo_clean.rs index cbeb3e61b..dbbf0412c 100644 --- a/src/cargo/ops/cargo_clean.rs +++ b/src/cargo/ops/cargo_clean.rs @@ -9,10 +9,10 @@ use sources::PathSource; use util::{CargoResult, human, ChainError, Config}; use ops::{self, Layout, Context, BuildConfig, Kind}; -pub struct CleanOptions<'a, 'b: 'a> { +pub struct CleanOptions<'a> { pub spec: Option<&'a str>, pub target: Option<&'a str>, - pub config: &'a Config<'b>, + pub config: &'a Config, } /// Cleans the project from build artifacts. diff --git a/src/cargo/ops/cargo_compile.rs b/src/cargo/ops/cargo_compile.rs index f2b276cf7..74137e9ef 100644 --- a/src/cargo/ops/cargo_compile.rs +++ b/src/cargo/ops/cargo_compile.rs @@ -37,8 +37,8 @@ use util::config::{ConfigValue, Config}; use util::{CargoResult, internal, human, ChainError, profile}; /// Contains informations about how a package should be compiled. -pub struct CompileOptions<'a, 'b: 'a> { - pub config: &'a Config<'b>, +pub struct CompileOptions<'a> { + pub config: &'a Config, /// Number of concurrent jobs to use. pub jobs: Option, /// The target platform to compile for (example: `i686-unknown-linux-gnu`). diff --git a/src/cargo/ops/cargo_doc.rs b/src/cargo/ops/cargo_doc.rs index 63ced9a12..5c2439e87 100644 --- a/src/cargo/ops/cargo_doc.rs +++ b/src/cargo/ops/cargo_doc.rs @@ -9,9 +9,9 @@ use ops; use sources::PathSource; use util::{CargoResult, human}; -pub struct DocOptions<'a, 'b: 'a> { +pub struct DocOptions<'a> { pub open_result: bool, - pub compile_opts: ops::CompileOptions<'a, 'b>, + pub compile_opts: ops::CompileOptions<'a>, } pub fn doc(manifest_path: &Path, diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index 2c97a3dd3..e12010439 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -10,8 +10,8 @@ use sources::{PathSource}; use util::config::{Config}; use util::{CargoResult, human}; -pub struct UpdateOptions<'a, 'b: 'a> { - pub config: &'a Config<'b>, +pub struct UpdateOptions<'a> { + pub config: &'a Config, pub to_update: Option<&'a str>, pub precise: Option<&'a str>, pub aggressive: bool, diff --git a/src/cargo/ops/cargo_rustc/context.rs b/src/cargo/ops/cargo_rustc/context.rs index 65e612f8f..3abb93e09 100644 --- a/src/cargo/ops/cargo_rustc/context.rs +++ b/src/cargo/ops/cargo_rustc/context.rs @@ -25,8 +25,8 @@ pub enum Platform { PluginAndTarget, } -pub struct Context<'a, 'b: 'a> { - pub config: &'a Config<'b>, +pub struct Context<'a> { + pub config: &'a Config, pub resolve: &'a Resolve, pub sources: &'a SourceMap<'a>, pub compilation: Compilation, @@ -49,16 +49,16 @@ pub struct Context<'a, 'b: 'a> { profiles: &'a Profiles, } -impl<'a, 'b: 'a> Context<'a, 'b> { +impl<'a> Context<'a> { pub fn new(resolve: &'a Resolve, sources: &'a SourceMap<'a>, deps: &'a PackageSet, - config: &'a Config<'b>, + config: &'a Config, host: Layout, target_layout: Option, root_pkg: &Package, build_config: BuildConfig, - profiles: &'a Profiles) -> CargoResult> { + profiles: &'a Profiles) -> CargoResult> { let target = build_config.requested_target.clone(); let target = target.as_ref().map(|s| &s[..]); let (target_dylib, target_exe) = try!(Context::filename_parts(target)); diff --git a/src/cargo/ops/cargo_rustc/fingerprint.rs b/src/cargo/ops/cargo_rustc/fingerprint.rs index 0b1ac8aa1..c73118490 100644 --- a/src/cargo/ops/cargo_rustc/fingerprint.rs +++ b/src/cargo/ops/cargo_rustc/fingerprint.rs @@ -39,11 +39,11 @@ pub type Preparation = (Freshness, Work, Work); /// This function will calculate the fingerprint for a target and prepare the /// work necessary to either write the fingerprint or copy over all fresh files /// from the old directories to their new locations. -pub fn prepare_target<'a, 'b>(cx: &mut Context<'a, 'b>, - pkg: &'a Package, - target: &'a Target, - profile: &'a Profile, - kind: Kind) -> CargoResult { +pub fn prepare_target<'a>(cx: &mut Context<'a>, + pkg: &'a Package, + target: &'a Target, + profile: &'a Profile, + kind: Kind) -> CargoResult { let _p = profile::start(format!("fingerprint: {} / {}", pkg.package_id(), target.name())); let new = dir(cx, pkg, kind); @@ -131,12 +131,12 @@ impl Fingerprint { /// /// Information like file modification time is only calculated for path /// dependencies and is calculated in `calculate_target_fresh`. -fn calculate<'a, 'b>(cx: &mut Context<'a, 'b>, - pkg: &'a Package, - target: &'a Target, - profile: &'a Profile, - kind: Kind) - -> CargoResult { +fn calculate<'a>(cx: &mut Context<'a>, + pkg: &'a Package, + target: &'a Target, + profile: &'a Profile, + kind: Kind) + -> CargoResult { let key = (pkg.package_id(), target, profile, kind); match cx.fingerprints.get(&key) { Some(s) => return Ok(s.clone()), diff --git a/src/cargo/ops/cargo_rustc/mod.rs b/src/cargo/ops/cargo_rustc/mod.rs index 5986693d0..24f160393 100644 --- a/src/cargo/ops/cargo_rustc/mod.rs +++ b/src/cargo/ops/cargo_rustc/mod.rs @@ -77,15 +77,15 @@ pub fn rustc_version() -> CargoResult<(String, String)> { // Returns a mapping of the root package plus its immediate dependencies to // where the compiled libraries are all located. -pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)], - pkg: &'a Package, - deps: &PackageSet, - resolve: &'a Resolve, - sources: &'a SourceMap<'a>, - config: &'a Config<'b>, - build_config: BuildConfig, - profiles: &'a Profiles) - -> CargoResult { +pub fn compile_targets<'a>(targets: &[(&'a Target, &'a Profile)], + pkg: &'a Package, + deps: &PackageSet, + resolve: &'a Resolve, + sources: &'a SourceMap<'a>, + config: &'a Config, + build_config: BuildConfig, + profiles: &'a Profiles) + -> CargoResult { if targets.is_empty() { return Ok(Compilation::new(pkg)) } @@ -181,10 +181,10 @@ pub fn compile_targets<'a, 'b>(targets: &[(&'a Target, &'a Profile)], Ok(cx.compilation) } -fn compile<'a, 'b>(targets: &[(&'a Target, &'a Profile)], - pkg: &'a Package, - cx: &mut Context<'a, 'b>, - jobs: &mut JobQueue<'a>) -> CargoResult<()> { +fn compile<'a>(targets: &[(&'a Target, &'a Profile)], + pkg: &'a Package, + cx: &mut Context<'a>, + jobs: &mut JobQueue<'a>) -> CargoResult<()> { debug!("compile_pkg; pkg={}", pkg); let profiling_marker = profile::start(format!("preparing: {}", pkg)); @@ -292,10 +292,10 @@ fn compile<'a, 'b>(targets: &[(&'a Target, &'a Profile)], Ok(()) } -fn prepare_init<'a, 'b>(cx: &mut Context<'a, 'b>, - pkg: &'a Package, - jobs: &mut JobQueue<'a>, - visited: &mut HashSet<&'a PackageId>) { +fn prepare_init<'a>(cx: &mut Context<'a>, + pkg: &'a Package, + jobs: &mut JobQueue<'a>, + visited: &mut HashSet<&'a PackageId>) { if !visited.insert(pkg.package_id()) { return } // Set up all dependencies diff --git a/src/cargo/ops/cargo_test.rs b/src/cargo/ops/cargo_test.rs index b4132239f..360d1b7d4 100644 --- a/src/cargo/ops/cargo_test.rs +++ b/src/cargo/ops/cargo_test.rs @@ -6,8 +6,8 @@ use sources::PathSource; use ops::{self, ExecEngine, ProcessEngine, Compilation}; use util::{self, CargoResult, ProcessError}; -pub struct TestOptions<'a, 'b: 'a> { - pub compile_opts: ops::CompileOptions<'a, 'b>, +pub struct TestOptions<'a> { + pub compile_opts: ops::CompileOptions<'a>, pub no_run: bool, } diff --git a/src/cargo/sources/git/source.rs b/src/cargo/sources/git/source.rs index c186c8e75..10a722cca 100644 --- a/src/cargo/sources/git/source.rs +++ b/src/cargo/sources/git/source.rs @@ -14,20 +14,20 @@ use sources::git::utils::{GitRemote, GitRevision}; /* TODO: Refactor GitSource to delegate to a PathSource */ -pub struct GitSource<'a, 'b:'a> { +pub struct GitSource<'cfg> { remote: GitRemote, reference: GitReference, db_path: PathBuf, checkout_path: PathBuf, source_id: SourceId, - path_source: Option>, + path_source: Option>, rev: Option, - config: &'a Config<'b>, + config: &'cfg Config, } -impl<'a, 'b> GitSource<'a, 'b> { +impl<'cfg> GitSource<'cfg> { pub fn new(source_id: &SourceId, - config: &'a Config<'b>) -> GitSource<'a, 'b> { + config: &'cfg Config) -> GitSource<'cfg> { assert!(source_id.is_git(), "id is not git, id={}", source_id); let reference = match source_id.git_reference() { @@ -141,7 +141,7 @@ pub fn canonicalize_url(url: &Url) -> Url { return url; } -impl<'a, 'b> Debug for GitSource<'a, 'b> { +impl<'cfg> Debug for GitSource<'cfg> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { try!(write!(f, "git repo at {}", self.remote.url())); @@ -152,7 +152,7 @@ impl<'a, 'b> Debug for GitSource<'a, 'b> { } } -impl<'a, 'b> Registry for GitSource<'a, 'b> { +impl<'cfg> Registry for GitSource<'cfg> { fn query(&mut self, dep: &Dependency) -> CargoResult> { let src = self.path_source.as_mut() .expect("BUG: update() must be called before query()"); @@ -160,7 +160,7 @@ impl<'a, 'b> Registry for GitSource<'a, 'b> { } } -impl<'a, 'b> Source for GitSource<'a, 'b> { +impl<'cfg> Source for GitSource<'cfg> { fn update(&mut self) -> CargoResult<()> { let actual_rev = self.remote.rev_for(&self.db_path, &self.reference); let should_update = actual_rev.is_err() || diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index 9ccad77ad..16681226e 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -12,19 +12,19 @@ use ops; use util::{self, CargoResult, internal, internal_error, human, ChainError}; use util::{MTime, Config}; -pub struct PathSource<'a, 'b: 'a> { +pub struct PathSource<'cfg> { id: SourceId, path: PathBuf, updated: bool, packages: Vec, - config: &'a Config<'b>, + config: &'cfg Config, } // TODO: Figure out if packages should be discovered in new or self should be // mut and packages are discovered in update -impl<'a, 'b> PathSource<'a, 'b> { - pub fn for_path(path: &Path, config: &'a Config<'b>) - -> CargoResult> { +impl<'cfg> PathSource<'cfg> { + pub fn for_path(path: &Path, config: &'cfg Config) + -> CargoResult> { trace!("PathSource::for_path; path={}", path.display()); Ok(PathSource::new(path, &try!(SourceId::for_path(path)), config)) } @@ -32,8 +32,8 @@ impl<'a, 'b> PathSource<'a, 'b> { /// Invoked with an absolute path to a directory that contains a Cargo.toml. /// The source will read the manifest and find any other packages contained /// in the directory structure reachable by the root manifest. - pub fn new(path: &Path, id: &SourceId, config: &'a Config<'b>) - -> PathSource<'a, 'b> { + pub fn new(path: &Path, id: &SourceId, config: &'cfg Config) + -> PathSource<'cfg> { trace!("new; id={}", id); PathSource { @@ -259,13 +259,13 @@ impl<'a, 'b> PathSource<'a, 'b> { } } -impl<'a, 'b> Debug for PathSource<'a, 'b> { +impl<'cfg> Debug for PathSource<'cfg> { fn fmt(&self, f: &mut Formatter) -> fmt::Result { write!(f, "the paths source") } } -impl<'a, 'b> Registry for PathSource<'a, 'b> { +impl<'cfg> Registry for PathSource<'cfg> { fn query(&mut self, dep: &Dependency) -> CargoResult> { let mut summaries: Vec = self.packages.iter() .map(|p| p.summary().clone()) @@ -274,7 +274,7 @@ impl<'a, 'b> Registry for PathSource<'a, 'b> { } } -impl<'a, 'b> Source for PathSource<'a, 'b> { +impl<'cfg> Source for PathSource<'cfg> { fn update(&mut self) -> CargoResult<()> { if !self.updated { let packages = try!(self.read_packages()); diff --git a/src/cargo/sources/registry.rs b/src/cargo/sources/registry.rs index 98d99576d..6dfdfcf60 100644 --- a/src/cargo/sources/registry.rs +++ b/src/cargo/sources/registry.rs @@ -180,14 +180,14 @@ use ops; static DEFAULT: &'static str = "https://github.com/rust-lang/crates.io-index"; -pub struct RegistrySource<'a, 'b:'a> { +pub struct RegistrySource<'cfg> { source_id: SourceId, checkout_path: PathBuf, cache_path: PathBuf, src_path: PathBuf, - config: &'a Config<'b>, + config: &'cfg Config, handle: Option, - sources: Vec>, + sources: Vec>, hashes: HashMap<(String, String), String>, // (name, vers) => cksum cache: HashMap>, updated: bool, @@ -226,9 +226,9 @@ struct RegistryDependency { kind: Option, } -impl<'a, 'b> RegistrySource<'a, 'b> { +impl<'cfg> RegistrySource<'cfg> { pub fn new(source_id: &SourceId, - config: &'a Config<'b>) -> RegistrySource<'a, 'b> { + config: &'cfg Config) -> RegistrySource<'cfg> { let hash = hex::short_hash(source_id); let ident = source_id.url().host().unwrap().to_string(); let part = format!("{}-{}", ident, hash); @@ -472,7 +472,7 @@ impl<'a, 'b> RegistrySource<'a, 'b> { } } -impl<'a, 'b> Registry for RegistrySource<'a, 'b> { +impl<'cfg> Registry for RegistrySource<'cfg> { fn query(&mut self, dep: &Dependency) -> CargoResult> { // If this is a precise dependency, then it came from a lockfile and in // theory the registry is known to contain this version. If, however, we @@ -511,7 +511,7 @@ impl<'a, 'b> Registry for RegistrySource<'a, 'b> { } } -impl<'a, 'b> Source for RegistrySource<'a, 'b> { +impl<'cfg> Source for RegistrySource<'cfg> { fn update(&mut self) -> CargoResult<()> { // If we have an imprecise version then we don't know what we're going // to look for, so we always atempt to perform an update here. diff --git a/src/cargo/util/config.rs b/src/cargo/util/config.rs index f2a592530..af567da54 100644 --- a/src/cargo/util/config.rs +++ b/src/cargo/util/config.rs @@ -18,9 +18,9 @@ use util::toml as cargo_toml; use self::ConfigValue as CV; -pub struct Config<'a> { +pub struct Config { home_path: PathBuf, - shell: RefCell<&'a mut MultiShell>, + shell: RefCell, rustc_version: String, /// The current host and default target of rustc rustc_host: String, @@ -29,8 +29,8 @@ pub struct Config<'a> { cwd: PathBuf, } -impl<'a> Config<'a> { - pub fn new(shell: &'a mut MultiShell) -> CargoResult> { +impl Config { + pub fn new(shell: MultiShell) -> CargoResult { let cwd = try!(env::current_dir().chain_error(|| { human("couldn't get the current directory of the process") })); @@ -72,7 +72,7 @@ impl<'a> Config<'a> { self.home_path.join("registry").join("src") } - pub fn shell(&self) -> RefMut<&'a mut MultiShell> { + pub fn shell(&self) -> RefMut { self.shell.borrow_mut() } diff --git a/src/cargo/util/toml.rs b/src/cargo/util/toml.rs index 08bf5537f..41786df9a 100644 --- a/src/cargo/util/toml.rs +++ b/src/cargo/util/toml.rs @@ -277,11 +277,11 @@ impl TomlProject { } } -struct Context<'a, 'b, 'c: 'b> { +struct Context<'a, 'b> { deps: &'a mut Vec, source_id: &'a SourceId, nested_paths: &'a mut Vec, - config: &'b Config<'c>, + config: &'b Config, } // These functions produce the equivalent of specific manifest entries. One -- 2.30.2